package com.selfdriving.activity;

import java.util.Date;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.dialog.ChooseDialog;
import com.selfdriving.dialog.ChooseDialog.OnBirthdayDialogClickListener;
import com.selfdriving.dialog.ChooseDialog.OnDialogClickListener;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.wl.activity.ActivityAboutMe;

public class AboutMeMyDatasSet extends BaseFragmentActivity implements
		OnClickListener
{
	ChooseDialog dialog;
	LinearLayout setName;
	LinearLayout setDate;
	LinearLayout setSex;
	private EditText setSign;
	private TextView setMyName;
	private TextView setMyBirthday;
	private TextView setMySex;
	private ImageView goBreak;
	private TextView topText;
	private Button top_right_but;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_my_set_datas);
		initViews();
		initEvents();
	}

	@Override
	protected void initViews()
	{
		top_right_but = (Button) findViewById(R.id.top_right_but);
		setName = (LinearLayout) findViewById(R.id.setName);
		setDate = (LinearLayout) findViewById(R.id.setDate);
		setSex = (LinearLayout) findViewById(R.id.setSex);
		setSign = (EditText) findViewById(R.id.setSign);
		setMyName = (TextView) findViewById(R.id.setMyName);
		setMyBirthday = (TextView) findViewById(R.id.setMyBirthday);
		setMySex = (TextView) findViewById(R.id.setMySex);
		goBreak = (ImageView) findViewById(R.id.head_break);
		goBreak.setVisibility(View.VISIBLE);
		topText = (TextView) findViewById(R.id.top_text_middle);
		topText.setText("修改资料");
		top_right_but.setText("确定");
		top_right_but.setVisibility(View.VISIBLE);
	}

	@Override
	protected void initEvents()
	{
		top_right_but.setOnClickListener(this);
		goBreak.setOnClickListener(this);
		setName.setOnClickListener(this);
		setDate.setOnClickListener(this);
		setSex.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.setName:
			dialog = new ChooseDialog(AboutMeMyDatasSet.this, "设置名字", setMyName
					.getText().toString());
			dialog.setOnDialogClickListener(new OnDialogClickListener()
			{
				@Override
				public void OnOkClick(String st)
				{
					if (st != null && !st.equals(""))
					{
						setMyName.setText(st);
					}
					dialog.dismiss();
				}

				@Override
				public void OnCancelClick()
				{
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
		case R.id.setDate:
			dialog = new ChooseDialog(mApplication, AboutMeMyDatasSet.this,
					"设置日期");
			dialog.setTitle("修改生日");
			dialog.setOnBirthdayDialogClickListener(new OnBirthdayDialogClickListener()
			{
				@Override
				public void OnOkClick(String st)
				{
					if (st != null && !st.equals(""))
					{
						setMyBirthday.setText(st);
					}
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
		case R.id.setSex:
			dialog = new ChooseDialog(mApplication, AboutMeMyDatasSet.this,
					"设置性别");
			dialog.setOnBirthdayDialogClickListener(new OnBirthdayDialogClickListener()
			{
				@Override
				public void OnOkClick(String st)
				{
					setMySex.setText(st);
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
		case R.id.top_right_but:
			try
			{
				postAlterDatas();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			break;
		case R.id.head_break:
			finish();
			break;
		}

	}

	public void postAlterDatas() throws Exception
	{
		String name = setMyName.getText().toString();
		String birthday = setMyBirthday.getText().toString();
		String sex = setMySex.getText().toString();
		String sign = setSign.getText().toString();
		Date date = Utils.dateFormat(birthday);
		String age = Utils.getAge(date);
		AjaxParams params = new AjaxParams();
		params.put("f", "69");
		params.put("phone", mApplication.user.getPhone());
		params.put("name", name);
		params.put("sign", sign);
		params.put("date", birthday);
		if (sex.equals("男"))
		{
			params.put("sex", "1");
		} else if (sex.equals("女"))
		{
			params.put("sex", "0");
		}
		params.put("age", age);
		showLoadingDialog("数据提交中...");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showShortToast("修改信息失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				showShortToast("信息提交成功");
				ActivityAboutMe.isLoadingDatas = true;
				dismissLoadingDialog();
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		finish();
	}
}
